public class BinarySearchTree {
    //首先这棵树是由若干个结点组成的
    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        //提供构造方法进行初始化

        public TreeNode(int val) {
            this.val = val;
        }
    }

    //根节点
    public TreeNode root;

    /**
     * search 搜索的意思
     *
     * @param val
     * @return
     */
    public boolean search(int val) {
        TreeNode cur = root;
        while (cur != null) {
            if (val > cur.val) {
                cur = cur.right;
            } else if (val < cur.val) {
                cur = cur.right;
            } else {
                return true;
            }
        }
        return false;
    }

    /**
     * 插入
     *
     * @param val
     */
    public void insert(int val) {

        //判断是否是空树
        if (root == null) {
            root = new TreeNode(val);
            return;
        }

        //定义一个前驱结点
        TreeNode parent = null;
        //定义一个临时节点
        TreeNode cur = root;

        while (cur != null) {
            if (val > cur.val) {
                parent = cur;
                cur = cur.right;
            } else if (val < cur.val) {
                parent = cur;
                cur = cur.left;
            } else {
                return;
            }
        }

        //插入的这个结点
        TreeNode node = new TreeNode(val);
        if (val > parent.val) {
            parent.right = node;
        }
        if (val < parent.val) {
            parent.left = node;
        }

    }

    /**
     * 删除这个结点
     * 利用替换的思路
     *
     * @param val
     */
    public void remove(int val) {
        //首先需要查找该树有没有该值
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null) {
            if (val > cur.val) {
                parent = cur;
                cur = cur.right;
            } else if (val < cur.val) {
                parent = cur;
                cur = cur.left;
            } else {
                //如果等于的话，说明该树有该值
                removeNode(parent, cur);
                return;
            }
        }
    }

    //进行覆盖的方法，删除结点
    private void removeNode(TreeNode parent, TreeNode cur) {
        if (cur.left == null) {
            if (cur == root) {
                root = cur.right;
            } else if (cur == parent.left) {
                parent.left = cur.right;
            } else {
                parent.right = cur.right;
            }
        } else if (cur.right == null) {
            if (cur == root) {
                root = cur.left;
            } else if (cur == parent.left) {
                parent.left = cur.left;
            } else {
                parent.right = cur.left;
            }
        } else {
            TreeNode t = cur.right;
            TreeNode tp = cur;
            while (t.left != null) {
                tp = t;
                t = t.left;
            }
            cur.val = t.val;
            if (tp.left == t) {
                tp.left = t.right;
            } else {
                tp.right = t.right;
            }
        }
    }

}
